package com.lyh.notes.controller;

import com.lyh.notes.model.base.ApiResponse;
import com.lyh.notes.model.base.EmptyVO;
import com.lyh.notes.model.dto.note.CreateNoteRequest;
import com.lyh.notes.model.dto.note.NoteQueryRequest;
import com.lyh.notes.model.dto.note.UpdateNoteRequest;
import com.lyh.notes.model.vo.note.*;
import com.lyh.notes.service.NoteService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import java.util.List;

/**
 * 笔记控制器
 */
@Log4j2
@RestController
@RequestMapping("/api")
public class NoteController {

    // 自动注入 NoteService 实例，用于处理笔记相关的业务逻辑
    @Autowired
    private NoteService noteService;

    /**
     * 查询笔记列表
     *
     */
    @GetMapping("/notes")
    public ApiResponse<List<NoteVO>> getNotes(
            @Valid@RequestBody NoteQueryRequest params) {
        return noteService.getNotes(params);
    }

    /**
     * 发布笔记
     *
     */
    @PostMapping("/notes")
    public ApiResponse<CreateNoteVO> createNote(
            @Valid @RequestBody CreateNoteRequest request) {
        return noteService.createNote(request);
    }

    /**
     * 更新笔记
     *
     */
    @PatchMapping("/notes/{noteId}")
    public ApiResponse<EmptyVO> updateNote(
            @Min(value = 1, message = "noteId 必须为正整数") @PathVariable Integer noteId,
            @Valid @RequestBody UpdateNoteRequest request) {
        return noteService.updateNote(noteId, request);
    }

    /**
     * 删除笔记
     *
     */
    @DeleteMapping("/notes/{noteId}")
    public ApiResponse<EmptyVO> deleteNote(
            @Min(value = 1, message = "noteId 必须为正整数")
            @PathVariable Integer noteId) {
        return noteService.deleteNote(noteId);
    }

    /**
     * 下载笔记
     */
    @GetMapping("/notes/download")
    public ApiResponse<DownloadNoteVO> downloadNote() {
        return noteService.downloadNote();
    }

    /**
     * 提交笔记排行榜
     */
    @GetMapping("/notes/ranklist")
    public ApiResponse<List<NoteRankListItem>> submitNoteRank() {
        return noteService.submitNoteRank();
    }

    /**
     * 用户提交热力图
     */
    @GetMapping("/notes/heatmap")
    public ApiResponse<List<NoteHeatMapItem>> submitNoteHeatMap() {
        return noteService.submitNoteHeatMap();
    }

    /**
     * 用户提交 top3 count
     */
    @GetMapping("/notes/top3count")
    public ApiResponse<Top3Count> submitNoteTop3Count() {
        return noteService.submitNoteTop3Count();
    }
}
